package com.angel.controller;

import com.angel.bean.OutWebResourceUrl;
import com.angel.exception.StatusException;
import com.angel.service.OtherlogService;
import com.angel.service.OutWebResourceUrlService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/outWebResourceUrlController")
public class OutWebResourceUrlController {

    @Autowired
    private OutWebResourceUrlService outWebResourceUrlService;
    @Autowired
    private OtherlogService otherlogService;


    /**
     * 删除
     *
     * @param outWebResourceUrl
     * @return
     */
    @RequestMapping("/delOutWebResourceUrl")
    @ResponseBody
    public Object delOutWebResourceUrl(OutWebResourceUrl outWebResourceUrl) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        outWebResourceUrl.setState(0);
        int result = outWebResourceUrlService.update(outWebResourceUrl);
        if (result != 0) {
            map.put("result", "success");
        }

        return map;
    }

    /**
     * 添加
     *
     * @param outWebResourceUrl
     * @return
     */
    @RequestMapping("/addOutWebResourceUrl")
    @ResponseBody
    public Object addOutWebResourceUrl(OutWebResourceUrl outWebResourceUrl) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        outWebResourceUrl.setState(1);
        int result = outWebResourceUrlService.insert(outWebResourceUrl);
        if (result != 0) {
            map.put("result", "success");
        }

        return map;
    }

    /**
     * 更新记录
     *
     * @param outWebResourceUrl
     * @return
     */
    @RequestMapping("/updateOutWebResourceUrl")
    @ResponseBody
    public Object updateOutWebResourceUrl(OutWebResourceUrl outWebResourceUrl) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        int result = outWebResourceUrlService.update(outWebResourceUrl);
        if (result != 0) {
            map.put("result", "success");
        }

        return map;
    }

    /**
     * 查询单个记录
     *
     * @param outWebResourceUrl
     * @return
     */
    @RequestMapping("/findOneOutWebResourceUrl")
    @ResponseBody
    public Object findOneOutWebResourceUrl(OutWebResourceUrl outWebResourceUrl) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        outWebResourceUrl.setState(1);
        OutWebResourceUrl outWebResUrl = outWebResourceUrlService.findOneOutWebResourceUrl(outWebResourceUrl);
        if (outWebResUrl != null) {
            map.put("outWebResUrl", outWebResUrl);
        }

        return map;
    }

    /**
     * 条件查询
     *
     * @param model
     * @return
     */
    @RequestMapping("/findOutWebResourceUrl")
    @ExceptionHandler(value = StatusException.class)
    public String findOutWebResourceUrl(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {

        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 5);
        OutWebResourceUrl outWebResourceUrl = new OutWebResourceUrl();
        outWebResourceUrl.setState(1);

        List<OutWebResourceUrl> outWebResourceUrlList = outWebResourceUrlService.findOutWebResourceUrl(outWebResourceUrl);

        PageInfo pageInfo = new PageInfo(outWebResourceUrlList);
        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "outWebResourceUrlEntry";
    }
}
